﻿using System;

using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;

namespace DataBaseManger
{
    public class SqlServerHelper:DataBaseHelper
    {
        private readonly string _ConnectionString;
        private bool _disposed = false;
        private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());

        public SqlServerHelper(string ConnectionString)
        {
            this._ConnectionString = ConnectionString;
            _provideName = "System.Data.SqlClient";
        }

        protected override DbConnection CreateConnection()
        {
            return new SqlConnection(_ConnectionString);
        }

        /// <summary>
        /// 将参数集合添加到缓存
        /// </summary>
        /// <param name="cacheKey">添加到缓存的变量</param>
        /// <param name="cmdParms">一个将要添加到缓存的sql参数集合</param>
        /// 
        public static void CacheParameters(string cacheKey, params SqlParameter[] commandParameters)
        {
            parmCache[cacheKey] = commandParameters;
        }

        /// <summary>
        /// 找缓存参数集合
        /// </summary>
        /// <param name="cacheKey">用于找回参数的关键字</param>
        /// <returns>缓存的参数集合</returns>
        public static SqlParameter[] GetCachedParameters(string cacheKey)
        {
            SqlParameter[] cachedParms = (SqlParameter[])parmCache[cacheKey];
            if (cachedParms == null)
                return null;

            SqlParameter[] clonedParms = new SqlParameter[cachedParms.Length];
            for (int i = 0, j = cachedParms.Length; i < j; i++)
                clonedParms[i] = (SqlParameter)((ICloneable)cachedParms[i]).Clone();

            return clonedParms;
        }



        ~SqlServerHelper()
        {
            try
            {
                if (_connection != null)
                    _connection.Close();
            }
            catch { }
            try
            {
                Dispose(true);
            }
            catch { }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        private void Dispose(bool disposing)
        {
            if (!this._disposed)
            {
                if (disposing)
                {
                    if (_connection != null)
                    {
                        RollbackTransaction();
                        Close();
                    }
                }
            }
            _disposed = true;
        }


        #region Transaction
        public void BeginTransaction()
        {
            _transaction = base._connection.BeginTransaction();
        }

        public void CommitTransaction()
        {
            try
            {
                if (_transaction != null)
                {
                    _transaction.Commit();
                    _transaction = null;
                }
            }
            catch { }
        }

        public void RollbackTransaction()
        {
            try
            {
                if (_transaction != null)
                {
                    _transaction.Rollback();
                    _transaction = null;
                }
            }
            catch { }
        }
        #endregion

    }
}
